package com.wangsd.system.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.wangsd.common.exception.BusinessErrorEnum;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.system.entity.SysMenu;
import com.wangsd.system.entity.SysRole;
import com.wangsd.system.entity.SysRoleMenu;
import com.wangsd.system.mapper.SysRoleMapper;
import com.wangsd.system.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色Service业务层处理
 *
 * @author wsd
 * @date 2021-03-16
 */
@Service
public class SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 查询角色
     *
     * @param id 角色ID
     * @return 角色
     */
    public SysRole selectSysRoleById(Long id) {
        return sysRoleMapper.getById(id);
    }

    /**
     * 查询角色列表
     *
     * @param sysRole 角色
     * @return 角色
     */
    public List<SysRole> selectSysRoleList(SysRole sysRole) {
        Query query = new Query();
        query.orderby("id", Sort.DESC);
        List<SysRole> list = sysRoleMapper.list(query);
        return list;
    }

    /**
     * 新增角色
     *
     * @param sysRole 角色
     * @return 结果
     */
    @Transactional
    public void insertSysRole(SysRole sysRole) {
        sysRoleMapper.saveIgnoreNull(sysRole);
        saveRoleMenu(sysRole);
    }

    /**
     * 修改角色
     *
     * @param sysRole 角色
     * @return 结果
     */
    @Transactional
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateIgnoreNull(sysRole);
        saveRoleMenu(sysRole);
    }

    /**
     * 删除角色信息
     *
     * @param id 角色ID
     * @return 结果
     */
    @Transactional
    public int deleteSysRoleById(Long id) {
        sysRoleMenuMapper.deleteByQuery(new Query().eq("role_id", id));
        return sysRoleMapper.deleteById(id);
    }

    /**
     * 保存角色和菜单的关联关系
     *
     * @param sysRole
     */
    private void saveRoleMenu(SysRole sysRole) {
        List<SysMenu> list = sysRole.getMenuList();
        if (list != null && list.size() > 0) {
            sysRoleMenuMapper.deleteByQuery(new Query().eq("role_id", sysRole.getId()));
            List<SysRoleMenu> roleMenus = new ArrayList<>();
            for (SysMenu entity : list) {
                //判断孩子有没有保存过
                boolean isSave = false;
                for (SysMenu child : entity.getChildren()) {
                    if (child.getActions() != null && child.getActions().size() > 0) {
                        isSave = true;
                        SysRoleMenu croleMenu = new SysRoleMenu();
                        croleMenu.setMenu_id(child.getId());
                        croleMenu.setRole_id(sysRole.getId());
                        roleMenus.add(croleMenu);

                        List<SysMenu> buttonlist = child.getButtonList();
                        for (String action : child.getActions()) {
                            SysRoleMenu broleMenu = new SysRoleMenu();
                            broleMenu.setMenu_id(getButtonId(buttonlist, action));
                            broleMenu.setRole_id(sysRole.getId());
                            roleMenus.add(broleMenu);
                        }
                    }
                }
                if (isSave) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setMenu_id(entity.getId());
                    roleMenu.setRole_id(sysRole.getId());
                    roleMenus.add(roleMenu);
                }
            }
            sysRoleMenuMapper.saveBatch(roleMenus);
        }
    }

    private Long getButtonId(List<SysMenu> buttonlist, String action) {
        if (buttonlist != null && buttonlist.size() > 0) {
            for (SysMenu button : buttonlist) {
                if (action.equals(button.getKeySign())) {
                    return button.getId();
                }
            }
        }
        return null;
    }

    /**
     * 判断名称是否重复
     *
     * @param name
     * @param id
     */
    public void checkName(String name, Long id) {
        Query query = new Query();
        query.eq("role_name", name);
        if (id != null) {
            query.notEq("id", id);
        }
        SysRole entity = sysRoleMapper.getByQuery(query);
        if (entity != null) {
            throw new BusinessException(BusinessErrorEnum.UNKNOWN_ERROR, "名称不能重复");
        }
    }

    /**
     * 获取下拉列表可展示的角色
     *
     * @param userId
     * @return
     */
    public List<SysRole> selectRoleByUserId(Long userId) {
        Query query = new Query();
        if (userId != 1L) {
            query.join("join sys_user_role t2 on t2.role_id = t.id");
            query.eq("t2.user_id", userId);
        }
        List<SysRole> list = sysRoleMapper.list(query);
        return list;
    }
}
